//商品上架
$(function(){
	//搜索
	$('.searchBar').click(function(){
		var thisform=$(this).parent('form');
		var pn=thisform.find('input[name=pn]').val();
		var name=thisform.find('input[name=name]').val();
		$('#packagematerialDatagrid').datagrid('options').pageNumber=1;//设置页码初始值为1
		$('#packagematerialDatagrid').datagrid({
			queryParams:{
				pn:pn,
				name:name
			}
		});
	})
	//更新行数据
	function updateActions(value){
		$.post(confirmEditpackagematerialUrl,{id:value.id,quantity:value.quantity,a_quantity:value.a_quantity},function(result){
            if (!result.status){
                $.messager.alert('错误提示',result.message,'error');
            }
        },'json').error(function(data){
        	var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				//$('#authAccessSetDialog').dialog('close');
			});
        });
	};

	var editIndex = undefined;
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#packagematerialDatagrid').datagrid('validateRow', editIndex)){
			$('#packagematerialDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			return true;
		} else {
			return false;
		}
	};
	//双击行编辑
	function onDblClickRow(index){
		if (editIndex != index){
			if (endEditing()){
				$('#packagematerialDatagrid').datagrid('selectRow', index);
				$('#packagematerialDatagrid').datagrid('endEdit', editIndex);
				$('#packagematerialDatagrid').datagrid('beginEdit', index);
				editIndex = index;
			} else {
				$('#packagematerialDatagrid').datagrid('selectRow', editIndex);
			}
		}
	};

	$('#packagematerialDatagrid').datagrid({
		//url:getpackagematerialUrl+"?randnum="+Math.floor(Math.random()*1000000),
		url:getpackagematerialUrl,
		title:'耗材管理',
		fitColumns:true,
		rownumbers:true,
		pageSize:25,
		pageNumber:1,
		pageList:[5,10,15,20,25,30,40,50],
		columns:[[
		{field:'id',title:'id',checkbox:true},
		{field:'pn',title:'条形码',sortable:true},
		{field:'name',title:'耗材名称'},
		{field:'quantity',title:'入库数量',sortable:true,
				editor: {
					type: 'numberbox',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'a_quantity',title:'出库数量',sortable:true,
				editor: {
					type: 'numberbox',  
					options: { 
						required: true,  
						missingMessage: '不能为空',
						min:1
					}  
				}
		},
		{field:'l_w_h',title:'长宽高',sortable:true},
		{field:'default',title:'默认',sortable:true,
				formatter:function(value,row,index){
					if(value=='1'){
						return '是';
					}else{
						return '否';
					}
				}
		}
		]],
		onDblClickRow: onDblClickRow,
		onAfterEdit:function(index,row){
					row.editing = false;
					updateActions(row);
		},
		onLoadError:function(data){
			var info=eval('('+data.responseText+')');
			$.messager.confirm('错误提示',info.message,function(r){
				var tab=$('#Tabs').tabs('getSelected');
				var index=$('#Tabs').tabs('getTabIndex',tab);
				$('#Tabs').tabs('close',index);
			});
		},
		pagination:true,
		fit:true,
	});
});
packagematerialObj={
	saveBar:function(){
		var editIndex = undefined;
		if (editIndex == undefined){endEditing = 1;}
		if ($('#packagematerialDatagrid').datagrid('validateRow', editIndex)){
			$('#packagematerialDatagrid').datagrid('endEdit', editIndex);
			editIndex = undefined;
			endEditing = 1;
		} else {
			endEditing = 0;
		}
		if (endEditing == 1){
			$('#packagematerialDatagrid').datagrid('acceptChanges');
		}
	},
	editBar:function(){
		var packagematerialRow=$('#packagematerialDatagrid').datagrid('getSelections');
        if (packagematerialRow.length>1){		            	
            $.messager.alert('提示','一次只能修改一条记录!','info');
        }else if(packagematerialRow.length==1){            	
        	$('#packagematerialEditDialog').dialog('open').dialog('setTitle','编辑商品');
            $('#packagematerialEditForm').form('load',packagematerialRow[0]);
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	setBar:function(value){
		var packagematerialRow=$('#packagematerialDatagrid').datagrid('getSelections');
        if(packagematerialRow.length>1){
            $.messager.alert('提示','只能设置一条记录!','info');
        }else if(packagematerialRow.length==1){
        	$.post(setdefault_packagematerial,{id:packagematerialRow[0].id,value:value},function(result){
				if (!result.status){
					$.messager.alert('错误提示',result.message,'error');
				}else{
					$('#packagematerialDatagrid').datagrid('reload'); 
				}
			},'json').error(function(data){
				var info=eval('('+data.responseText+')');
				$.messager.confirm('错误提示',info.message,function(r){
					//$('#authAccessSetDialog').dialog('close');
				});
			});
        }else{
			$.messager.alert('推送提示','请选择列表数据','error');
		}
	},
	save:function(url){
		$('#packagematerialEditForm').form('submit',{
            url: url,
            onSubmit: function(){
                return $(this).form('validate');
            },
            success: function(result){
            	 var result=eval('('+result+')');
                if (!result.status){
                    $.messager.confirm('错误提示',result.message,function(r){
                    	$('#packagematerialEditDialog').dialog('close');
                    });
                }else{
                    $('#packagematerialEditDialog').dialog('close');      
                    $('#packagematerialDatagrid').datagrid('reload');    
                }
            }
        });
	},
	showAll:function(){
		$('#packagematerialDatagrid').datagrid({ queryParams:''});
	},
	reloadBar:function(){
		$('#packagematerialDatagrid').datagrid('reload');
	}

}